<?php

namespace App\Models\Admin;

use App\Log\GoodsAdminLog;
use App\Scopes\SaasScope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis;
use Saas;

class Goods extends Model
{
    const OPERATE_PID = 5;
    const MODEL = 'goods';
    const STATUS_LIST = [0 => '下架', 1 => '上架',];
    const STANDARD_LIST = [0 => '非标品', 1 => '标品',];
    const RECOMMEND_LIST = [0 => '否', 1 => '是',];


    protected $table = 'goods';

    protected $dateFormat = 'U';

    public $timestamps = true;

    protected $guarded = [];

    public function spu()
    {
        return $this->hasMany('App\Models\GoodsSpu','goods_id','id');
    }

    public function category()
    {
        return $this->hasOne(GoodsCategory::class,'id','category_id');
    }

    public function category_p()
    {
        return $this->hasOne(GoodsCategory::class,'id','pid');
    }

    public function detailInfo()
    {
        return $this->hasOne(GoodsDetail::class,'goods_id','id');
    }

    public function images()
    {
        return $this->hasMany(GoodsImage::class,'goods_id','id')->orderBy('type','ASC');
    }

    public function purchaseRelation()
    {
        return $this->hasMany(PurchaseRelation::class,'goods_id','id');
    }

    public static function is_del($id=0):array
    {
        $info = self::find($id);
        $info->is_del = 1;
        GoodsSpu::where('goods_id',$id)->update(['is_del'=>1]);
        if ($info->save()){
            return ['success','0','删除成功'];
        }
        return ['error','40011','删除失败'];
    }

    public static function addLog($pre,$next,$type)
    {
        $info = OperateMoudel::where('pid',self::OPERATE_PID)->where('type',$type)->where('model',self::MODEL)->first();
        $goodsLog = new GoodsAdminLog($pre,$next,$info);
        $goodsLog->addLog();
    }


    public static function getPrice($uid,$group_id,$goods_id,$goods_spu_id):float
    {
        $time = time();
        $cycle = CyclePrice::where('start_time','<',$time)->where('end_time','>',$time)->where('status',1)->whereRaw("FIND_IN_SET($uid,uids)")->first();
        $price = 0;
        if ($cycle) {//周期报价的价格
            $cyclePriceGoods = CyclePriceGoods::where('cycle_id', $cycle->id)->where('goods_id', $goods_id)->where('goods_spu_id', $goods_spu_id)->first();
            if (!empty($cyclePriceGoods)){
                //return $this->success(['price'=>$cyclePriceGoods->price]);
                return $cyclePriceGoods->price;
            }
        }
        $key = 'offer_'.Saas::id().'_'.$goods_id.'_'.$goods_spu_id.'_'.$group_id;
        if ($price = Redis::hmget($key,['price'])[0]){//分组报价的价格，redis有的话
            //return $this->success(['price'=>$price]);
            return $price;
        }
        //redis 没有查询数据库
        $goodsOffer = GoodsOffer::where('goods_id',$goods_id)->where('goods_spu_id',$goods_spu_id)->where('group_id',$group_id)->first();

        if (empty($goodsOffer)){//没有特殊报价则返回商品市场价
            $price = GoodsSpu::find($goods_spu_id)->market_price;
        }else{
            $price = $goodsOffer->price;
        }
        return $price;
    }
}
